using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace iPodConverter
{
    public class ffmpeg
    {
        ProcessStartInfo procInfo;
        public Process ffmpegProcess;

        

        public ffmpeg()
        {
            procInfo = new ProcessStartInfo();
            procInfo.CreateNoWindow = false;
            procInfo.UseShellExecute = true;
            procInfo.WorkingDirectory = System.Windows.Forms.Application.StartupPath;
            procInfo.FileName = "ffmpeg.exe";
            showFfmpegOutput = true;
          
        }

        private bool ShowFFmpegOutput;

        public bool showFfmpegOutput
        {
            get { return ShowFFmpegOutput; }
            set { ShowFFmpegOutput = value; }
        }
        
        private string inputFileName;

        /// <summary>
        /// Gets or sets the name of the input file.
        /// </summary>
        /// <value>The name of the input file.</value>
        public string InputFileName
        {
            get { return inputFileName; }
            set { inputFileName = value; }
        }

        private string outputFileName;

        /// <summary>
        /// Gets or sets the name of the output file.
        /// </summary>
        /// <value>The name of the output file.</value>
        public string OutputFileName
        {
            get { return outputFileName; }
            set { outputFileName = value; }
        }

        private string videoTitle;
        public string VideoTitle
        {
            get { return videoTitle; }
            set { videoTitle = value; }
        }
	
        private int percentComplete = 50;
        /// <summary>
        /// Gets the percentage of the conversion that is complete.
        /// </summary>
        /// <value>The percent complete.</value>
        public int PercentComplete
        {
            
            get { return percentComplete; }
            
        }

        private string targetResolution;

        public string TargetResolution
        {
            get { return targetResolution; }
            set { targetResolution = value; }
        }
	

        public void ConvertFile()
        {

            procInfo.Arguments = "-i \"" + inputFileName + "\" -acodec libfaac -async 2 -r 30000/1001 -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s " + targetResolution + " -title \"" + videoTitle + "\" \"" + outputFileName + "\"";

            //procInfo.Arguments2 = "-i \"" + inputFileName + "\" -async 2 -acodec libfaac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s " + targetResolution + " -title \"" + videoTitle + "\" \"" + outputFileName + "\"";

            
            
            //// converts xvid properley
            //procInfo.Arguments3 = "-i \"" + inputFileName + "\" -r 30000/1001 -acodec libfaac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s " + targetResolution + " -title \"" + videoTitle + "\" \"" + outputFileName + "\"";



            // Hide the window if the user doesn't want to see it.
            if (!showFfmpegOutput)
            {
                procInfo.WindowStyle = ProcessWindowStyle.Hidden;
            }
            

            procInfo.UseShellExecute = false;
            ffmpegProcess = new Process();
            ffmpegProcess.StartInfo = procInfo;
            
            ffmpegProcess.Start();
 

      
 
        }

       

 


        public void CancelConversion()
        {
            
            Thread.Sleep(500);
            while (!ffmpegProcess.HasExited)
            {
                ffmpegProcess.Kill();
                Thread.Sleep(500);
            }
        }

    }
}
